function jsonstringify(obj, space) {
    space = space || '';
    var stringify = function(val, step) {
        var type = typeof val;
        if ('object' !== type) {
            if(/string|undefined|function/.test(type)){
                val = '"' + val + '"'
            }
            return val + '';
        }
        var bSpace = space ? '\n' + new Array(step + 1).join(space) : '';
        var aSpace = bSpace + space;
        var isArray = val instanceof Array;
        var jsons = [];
        for (var key in val) {
            jsons.push((isArray ? '' : '"' + key + '":') + stringify(val[key], step + 1));
        }
        return (isArray ? '[' : '{') + aSpace + jsons.join(',' + aSpace) + bSpace + (isArray ? ']' : '}')
    };
    return stringify(obj, 0);
}

function initItems(flag) {
    var items = g.items;
    for (var key in items) {
        if(items.hasOwnProperty(key) && (!flag || key[0] != '_')){
            var item = items[key];
            item.val = item.init || 0;
            item.time = 0;
        }
    }
}

function keys() {
    var keys = [];
    for (var key in g.items) {
        if(key[0] != '_'){
            keys.push(key);
        }
    }
    return keys
}

function checkTime(key){
  var item = g.items[key];
  if(item.interval){
    var now = new Date().getTime()
    if(item.time && now - item.time < item.interval){
      return false;
    }
    item.time = now;
  }
  return true;
}

function checkLimit(key){
  var item = g.items[key];
  return item.val < item.limit;
}

function checkDate(){
  var newDate = new Date().toLocaleDateString();
  if(newDate != g.startDate){
    g.startDate = newDate;
    initItems();
    g.items._step.interval = 0;
    return true;
  }
  return false;
}

function checkStep(){
  var step = g.items._step;
  if(step.val < step.limit && step.val <= step.interval){
    return step.val++;
  }
  return -1;
}

function updateStep(){
  var step = g.items._step;
  if(step.interval < step.limit){
    step.interval++;
    initItems(true);
    return true;
  }
  return false;
}

function initStep(){
  var step = g.items._step;
  if(step.val != step.interval){
    step.val = step.interval;
    return true;
  }
  return false
}